import VideoAdInterface from "../base/VideoAdInterface";

export default class VivoRewardAd implements VideoAdInterface {


    private rewardedVideoAd: any;

    private isFinish: boolean = false;

    private rewardCallback: (isFinish: boolean) => void;

    constructor(id:string){
        this.createVideoAd(id,(err)=>{
            if(err){
                this.isFinish = false
            }else{
                this.isFinish = true;
            }
        });

    }

    isLoad(): boolean {
        if (qg.getSystemInfoSync().platformVersionCode < 1031) {
            // 不支持广告
            return false
        }
        return this.isFinish;
    }

    createVideoAd(id: string, callback: Function): void {
        this.rewardedVideoAd = qg.createRewardedVideoAd({
            posId: id
        });
        this.rewardedVideoAd.onLoad(() => {
            console.log("激励视频已加载");
            // this.isFinish = true;
            callback(null)
        });
        this.rewardedVideoAd.onError(err => {
            console.log("激励视频异常", err);
            callback(err)
        });
        this.rewardedVideoAd.onClose(res => {
            if (res && res.isEnded) {
                console.log("正常播放结束，可以下发游戏奖励");
                this.rewardCallback(true)
            } else {
                console.log("播放中途退出，不下发游戏奖励");
                this.rewardCallback(false)
            }
            this.load();
        });
    }

    load() {
        this.rewardedVideoAd.load().then(() => {
            console.log("激励视频广告加载成功");
        }).catch(err => {
            console.log("激励视频广告加载失败", err);
        });
    }

    show(callback: (isFinish: boolean) => void): void {
        this.rewardCallback = callback;
        this.rewardedVideoAd.show().then(() => {
            console.log("激励视频广告显示成功");
        }).catch(err => {
            console.log("激励视频广告显示失败", err);
        });
    }

    callback(num: number) {

    }


}
